package controller

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service/redemption"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Redemption struct{}

func (r Redemption) Add(c *gin.Context) {
	var params struct {
		AddNum         int     `json:"add_num"`
		Value          float64 `json:"value"`
		ExpirationTime string  `json:"expiration_time"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	_, err := redemption.RedemptionService{}.Add(c.Request.Context(), db, merchantID, params.AddNum, params.Value, params.ExpirationTime, "")
	response.JSON(c, nil, err)
}

func (r Redemption) Remove(c *gin.Context) {
	var params struct {
		ShopID         string `json:"shop_id"`
		RedemptionCode string `json:"redemption_code"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	err := redemption.RedemptionService{}.Remove(c.Request.Context(), db, params.ShopID, params.RedemptionCode)
	response.JSON(c, nil, err)
}

func (r Redemption) GetRC(c *gin.Context) {
	var params struct {
		ShopID          *[]string `json:"shop_id"`
		Page            int       `json:"page"`
		Limit           int       `json:"limit"`
		RedemptionCode  string    `json:"redemption_code"`
		RedemptionState *int      `json:"redemption_state"`
		BatchID         string    `json:"batch_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	var shopIDList = []string{}
	if params.ShopID != nil {
		shopIDList = append(shopIDList, *params.ShopID...)
	}

	data, count, err := redemption.RedemptionService{}.GetRC(c.Request.Context(), db, merchantID, shopIDList, params.Page, params.Limit,
		params.RedemptionCode, params.RedemptionState, params.BatchID)
	response.JSON(c, gin.H{
		"list":  data,
		"count": count,
	}, err)
}

func (r Redemption) GetLog(c *gin.Context) {
	var params struct {
		ShopID *[]string `json:"shop_id"`
		Page   int       `json:"page"`
		Limit  int       `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	var shopIDList = []string{}
	if params.ShopID != nil {
		shopIDList = append(shopIDList, *params.ShopID...)
	}

	data, count, err := redemption.RedemptionService{}.GetLog(c.Request.Context(), db, merchantID, shopIDList, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  data,
		"count": count,
	}, err)
}
